<template>
    <div class="classify-lists">
        <el-card class="!border-none" shadow="never">
            <div class="flex justify-between mb-4">
                <div class="flex items-center">
                    <el-input
                        v-model="queryParams.name"
                        placeholder="请输入分类名称"
                        clearable
                        class="w-[280px] mr-4"
                        @keyup.enter="resetPage"
                    />
                    <el-button type="primary" @click="resetPage">
                        <template #icon>
                            <icon name="el-icon-Search" />
                        </template>
                        搜索
                    </el-button>
                    <el-button @click="resetParams">
                        <template #icon>
                            <icon name="el-icon-Refresh" />
                        </template>
                        重置
                    </el-button>
                </div>
                <div>
                    <el-button
                        v-perms="['shop:category:add']"
                        type="primary"
                        @click="handleAdd"
                    >
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增分类
                    </el-button>
                </div>
            </div>
            <el-table
                v-loading="pager.loading"
                :data="pager.lists"
                :height="tableHeight"
                style="width: 100%"
            >
                <el-table-column label="分类ID" prop="id" width="80" />
                <el-table-column label="分类名称" prop="name" min-width="150" />
                <el-table-column label="分类图标" width="100">
                    <template #default="{ row }">
                        <el-image
                            v-if="row.icon"
                            :src="row.icon"
                            :preview-src-list="[row.icon]"
                            class="w-12 h-12 rounded"
                            fit="cover"
                        />
                        <span v-else class="text-tx-secondary">暂无</span>
                    </template>
                </el-table-column>
                <el-table-column label="排序" prop="sort" width="80" />
                <el-table-column label="状态" width="100">
                    <template #default="{ row }">
                        <el-tag :type="row.status === 1 ? 'success' : 'danger'">
                            {{ row.status === 1 ? '启用' : '禁用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['shop:category:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['shop:category:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>

        <!-- 编辑弹窗 -->
        <classify-edit v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { apiShopCategoryLists, apiShopCategoryDelete } from '@/api/shop'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import ClassifyEdit from './classifyEdit.vue'

const editRef = shallowRef<InstanceType<typeof ClassifyEdit>>()
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    name: ''
})

// 分页相关
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: apiShopCategoryLists,
    params: queryParams
})

// 自适应表格高度
const tableHeight = ref(400)

// 计算表格高度
const calculateTableHeight = () => {
    const windowHeight = window.innerHeight
    const headerHeight = 60 // 顶部导航高度
    const cardPadding = 48 // 卡片内边距
    const searchHeight = 60 // 搜索区域高度
    const paginationHeight = 60 // 分页区域高度
    const margin = 40 // 额外边距
    
    tableHeight.value = windowHeight - headerHeight - cardPadding - searchHeight - paginationHeight - margin
}

// 添加分类
const handleAdd = () => {
    showEdit.value = true
    nextTick(() => {
        editRef.value?.open('add')
    })
}

// 编辑分类
const handleEdit = (row: any) => {
    showEdit.value = true
    nextTick(() => {
        editRef.value?.open('edit', row)
    })
}

// 删除分类
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除该分类吗？')
    await apiShopCategoryDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

// 监听窗口大小变化
const handleResize = () => {
    calculateTableHeight()
}

onMounted(() => {
    calculateTableHeight()
    window.addEventListener('resize', handleResize)
    getLists()
})

onUnmounted(() => {
    window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.classify-lists {
    height: 100%;
    
    :deep(.el-card__body) {
        height: 100%;
        display: flex;
        flex-direction: column;
    }
}
</style>